package controller;

import service.serviceimpl.ServiceUserImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @Author wuchao66
 * @Date 2023/5/25 15:20
 * @PackageName:${PACKAGE_NAME}
 * @ClassName: ${NAME}
 * @Description: TODO
 * @Param
 * @Version 1.0
 */
//1告诉电脑你想在哪里成为servlet 2怎么调用你
//@WebServlet(name ="T163Servlet",urlPatterns = "/T163Servlet",
//initParams = {@WebInitParam(name = "name",value = "超哥"),
//@WebInitParam(name="name1",value = "超哥真腻害")})
@WebServlet(name ="T163Servlet",urlPatterns = "/T163Servlet")
//注解 标注解释说明
public class T163Servlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();//文件流

        out.flush();
        out.close();
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //乱码规则
        response.setCharacterEncoding("utf-8");
        //文件流
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();//文件流
        //定义全局的op来判断curd,对应操作
        String op = request.getParameter("op");//先拿标志
        if (op != null) {//有标志，可以判断curd
            ServiceUserImpl serviceUser = new ServiceUserImpl();
            if (op.equals("delete")) {//做删除
                //拿到删除id
                int id= Integer.parseInt(request.getParameter("id"));
                //执行删除操作
                int i = serviceUser.executeUpdate("delete from userinfo where id=?", id);
                if(i>0){//删除成功
//                    HttpSession session = request.getSession();
//                    session.setAttribute("xx1","wer");
//                    ${sessionScope.xx}
//                    ${xx1}
//                    ${xx}
                    request.setAttribute("xx","完了xxyy");
                    request.setAttribute("delete1","t");
                    request.getRequestDispatcher("/jspnc1/main.jsp").forward(request,response);
                }else{//删除失败
                    response.sendRedirect(request.getContextPath()+"/jspnc1/main.jsp?delete2=t");
                }
            }
            else if(op.equals("add")){
//                System.out.println( this.getServletConfig().getInitParameter("name"));
//                System.out.println( this.getServletConfig().getInitParameter("name1"));
               // this.getServletContext().getInitParameter("adminEmail");

//                //着手增加
                String name = request.getParameter("name");
                String dept = request.getParameter("dept");
                int sex= Integer.parseInt(request.getParameter("sex"));
                int i=serviceUser.executeUpdate("insert into userinfo(name,sex,age,ranking,dept) values(?,?,18,'1',?) "
                        ,name,sex,dept);
                if(i>0){//增加成功
                    request.getRequestDispatcher("/jspnc1/main.jsp?add1=t").forward(request,response);
                }else{//增加失败
                    response.sendRedirect(request.getContextPath()+"/jspnc1/main.jsp?add2=t");
                }
            }else if(op.equals("update1")){
                //拿到传入要修改的id
                int id= Integer.parseInt(request.getParameter("id"));
                response.sendRedirect("update.jsp?id="+id);
            }else if(op.equals("update2")){
                int id= Integer.parseInt(request.getParameter("id"));
                String name = request.getParameter("name");
                String dept = request.getParameter("dept");
                int sex= Integer.parseInt(request.getParameter("sex"));
                int i = serviceUser.executeUpdate("update userinfo set name=?,sex=?,dept=? where id=?", name, sex, dept, id);
                if(i>0){//修改成功
                    request.getRequestDispatcher("/jspnc1/main.jsp?update1=t").forward(request,response);
                }else{//修改失败
                    response.sendRedirect(request.getContextPath()+"/jspnc1/main.jsp?update2=t");
                }

            }

        } else {
            response.sendRedirect("main.jsp?feifa=t");
        }

//        String name = request.getParameter("name");
//        System.out.println(name);
//        if(name.equals("1")){
//            //请求时不断的，能记住回去的路，重定向他不知道回去的路
//            request.getRequestDispatcher("/demo6/main.jsp?id=1").forward(request,response);
//        }else{//问request 怎么回去
//            response.sendRedirect(request.getContextPath()+"/demo6/main.jsp?id=2");
//        }

        out.flush();
        out.close();
    }
}
